package com.hky.maker.generator;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class ScriptGenerator {

    public static void doGenerate(String outputPath, String jarPath) {

        StringBuilder sb = new StringBuilder();
        // Linux 脚本
        if (System.getProperty("os.name").toLowerCase().contains("linux")) {
            sb.append("#!/bin/bash").append("\n");
            sb.append(String.format("java -jar %s \"$@\"", jarPath)).append("\n");
            FileUtil.writeBytes(sb.toString().getBytes(StandardCharsets.UTF_8), outputPath);
            // 设置脚本执行权限
            try {
                Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxrwxrwx");
                Files.setPosixFilePermissions(Paths.get(outputPath), permissions);
            } catch (IOException e) {

            }
        }


        // windows 脚本
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            sb = new StringBuilder();
            sb.append(" @echo off").append("\n");
            sb.append(String.format("java -jar %s %%*", jarPath)).append("\n");
            FileUtil.writeBytes(sb.toString().getBytes(StandardCharsets.UTF_8), outputPath + ".bat");
        }

    }
}
